app.service('shopDetailInfoService',['$q','utilService','$http','$cookies',function($q,utilService,$http,$cookies){
    var _this_=this;

    _this_.scoreState='';
    //查询店铺信息
    _this_.findinfo= function ($scope) {
        var deferred = $q.defer();
        utilService.get({
            url:$scope.app.domain + 'shop/id'
        }).then(function(response){
            deferred.resolve(response.data);
        },function(e){
            console.error(e);
            deferred.reject("连接服务器出错！");
        })

        return deferred.promise;
    }

    //查询店铺评价总分
    _this_.getShopEvaluateScore= function ($scope) {
        var deferred = $q.defer();
        utilService.get({
            url:$scope.app.domain + 'user/evaluate/score'
        }).then(function(response){
            deferred.resolve(response.data);
        },function(e){
            console.error(e);
            deferred.reject("连接服务器出错！");
        })
        return deferred.promise;
    }


    //查询评价列表
    _this_.findAll=function($scope){
        var deferred = $q.defer();
        utilService.get({
            url:$scope.app.domain + 'user/evaluate/',
            params:{start:$scope.page*$scope.length,draw:1,length:$scope.length,scoreState:_this_.scoreState}
        }).then(function(response){
            deferred.resolve(response.data);
        },function(e){
            console.error(e);
            deferred.reject("连接服务器出错！");
        });
        return deferred.promise;
    }

    _this_.createQrcode=function($scope,shopId){
        var deferred = $q.defer();
        utilService.api({
            url:$scope.app.domain+"shop/create/qrcode",
            data:{shopId:shopId},
            method:'post'
        }).then(function(result){
            deferred.resolve(result);
        });
        return deferred.promise;
    }

    _this_.downloadQrcode=function($scope,shopId){
        $http({
            method:'post',
            url:$scope.app.domain + 'download',
            data:{sessionId:$cookies.sessionId,filefullPath:"shop/qrcode/"+shopId+".png"},
            responseType: "arraybuffer",
            headers:{'Content-Type': 'application/x-www-form-urlencoded;'},
            transformRequest: function(obj) {
                var str = [];
                for(var p in obj){
                    str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
                }
                return str.join("&");
            }
        }).success(function(data){
            var blob = new Blob([data], {type: "application/vnd.ms-excel"});
            var fileName ="qrcode"+ (new Date()).getTime()+'.png';
            var a = document.createElement("a");
            document.body.appendChild(a);
            a.download = fileName;
            a.href = URL.createObjectURL(blob);
            a.click();
        })
    }
}]);